// Copyright (c) 2009-2010, Jeremy Brewer
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package galaxie500.datastructures;

/**
 * Class for representing multidimensional rectangular bounding boxes.
 * 
 * @author Jeremy Brewer
 */
public class BoundingBox {
  /** Array of minimum values for the box */
  private double[] minValues;

  /** Array of maximum values for the box */
  private double[] maxValues;

  /**
   * Creates an empty BoundingBox.  Must call reset() to be useful.
   */
  public BoundingBox() {
  }

  /**
   * Creates a new instance of a bounding box with boundaries given by
   * minValues[i], maxValues[i] for i in (0, dimension) of the box.
   * 
   * @param minValues The array of minimum values, size dimension of the box
   * @param maxValues The array of maximum values, size dimension of the box
   */
  public BoundingBox(double[] minValues, double[] maxValues) {
    reset(minValues, maxValues);
  }

  /**
   * Resets the internal arrays of boundaries without creating a new instance.
   * 
   * @param minValues The array of minimum values, size dimension of the box
   * @param maxValues The array of maximum values, size dimension of the box
   */
  public void reset(double[] minValues, double[] maxValues) {
    assert minValues.length == maxValues.length;
    this.minValues = minValues;
    this.maxValues = maxValues;
  }

  /**
   * Returns the dimensionality of this box.
   * 
   * @return The dimension of the box
   */
  public int dimension() {
    return minValues.length;
  }

  /**
   * Checks whether this bounding box contains the given point.
   * 
   * @param point The point to test
   * @return Whether the point lies inside this bounding box
   */
  public boolean contains(Point point) {
    assert minValues.length == point.dimension();
    for (int i = 0; i < point.dimension(); i++) {
      double value = point.getValue(i);
      if (value > maxValues[i] || value < minValues[i]) {
        return false;
      }
    }
    return true;
  }

  /**
   * Checks whether this bounding box intersects another bounding box.
   * 
   * @param box The bounding box to test
   * @return Whether the bounding box intersects this bounding box
   */
  public boolean intersects(BoundingBox box) {
    assert minValues.length == box.minValues.length;
    for (int i = 0; i < minValues.length; i++) {
      if (minValues[i] > box.maxValues[i] || maxValues[i] < box.minValues[i]) {
        return false;
      }
    }
    return true;
  }

  /**
   * Returns the largest dimension of the bounding box.
   * 
   * @return The largest dimension of the box or -1 if all dimensions are size 0
   *         (i.e. the 'box' is really just a point).
   */
  public int largestDimension() {
    // Use the largest dimension as the pivot dimension, and use the midpoint
    // of the box as the pivot value.
    int maxDimension = 0;
    double maxSize = maxValues[0] - minValues[0];

    for (int i = 1; i < minValues.length; i++) {
      double size = maxValues[i] - minValues[i];
      if (size > maxSize) {
        maxDimension = i;
        maxSize = size;
      }
    }

    if (maxSize == 0.0) return -1; // Box is just one point repeated.
    return maxDimension;
  }
}
